#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/wait.h>
#include<stdlib.h>
#define NUM 128
#define CMD_NUM 64
int main()
{
  char command[NUM];
  for( ; ;  )
  {
   char* argv[CMD_NUM]={NULL};    
     command[0]=0;
    printf("[wkd@hostname mydir]# ");
    fflush(stdout);
    fgets(command,NUM,stdin);
    command[strlen(command)-1]='\0';
    const char* sep=" ";
    argv[0]=strtok(command,sep);
    int i=1;
    while(argv[i]=strtok(NULL,sep))
    {
      i++;
    }
  if(strcmp(argv[0],"cd")==0)
  {
    if(argv[1]!=NULL)
      chdir(argv[1]);
     continue;
  }
  if(fork()==0)
  {
    execvp(argv[0],argv);
    exit(1);
  }
  int status=0;
  waitpid(-1,&status,0);
  printf("exit code:%d\n",(status>>8)&0xFF);
  }
  return 0;
}

